// resource/components/list.vue
<template>
    <!-- 使用 Card 组件 -->
    <el-card>
      <div slot="header" class="clearfix">
        <!-- 使用 Form 组件：行内表单 -->
        <el-form
        :inline="true"
        :model="form"
        class="demo-form-inline"
        ref="form">
          <el-form-item label="资源名称" prop="name">
            <el-input
            v-model="form.name"
            placeholder="资源名称"
            clearable></el-input>
          </el-form-item>
          <el-form-item label="资源路径" prop="url">
            <el-input
            v-model="form.url"
            placeholder="资源路径"
            clearable></el-input>
          </el-form-item>
          <el-form-item label="资源分类" prop="categoryId">
            <el-select
            v-model="form.categoryId"
            placeholder="资源分类"
            clearable>
              <!-- 资源分类数据请求 -->
              <el-option
              :label="item.name"
              :value="item.id"
              v-for="item in resourceCategories"
              :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
            type="primary"
            @click="onSubmit"
            :disabled="isLoading">查询</el-button>
            <el-button
            @click="onReset"
            :disabled="isLoading">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div slot="header" class="clearfix">
        <el-button  @click="handleAdd">添加</el-button>
        <el-button type="success" @click="$router.push({ name: 'resourceCategory' })">资源分类</el-button>
      </div>
        <!-- 使用 Table 组件 -->
        <el-table
          :data="resources"
          style="width: 100%"
          v-loading="isLoading">
          <el-table-column
            label="编号"
            width="100"
            type="index">
          </el-table-column>
          <el-table-column
            prop="name"
            label="资源名称">
          </el-table-column>
          <el-table-column
            prop="url"
            label="资源路径">
          </el-table-column>
          <el-table-column
            prop="description"
            label="描述">
          </el-table-column>
          <!-- 时间 -->
          <el-table-column
            label="添加时间">
            <template slot-scope="scope">
              <span>{{ scope.row.createdTime | dateFormat}}</span>
            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.row)">编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页功能 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="form.current"
          :page-sizes="[5, 10, 20]"
          :page-size="form.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount"
          :disabled="isLoading">
        </el-pagination>
        <!-- Dialog对话框 -->
        <el-dialog
          :title="isEdit ? '编辑资源' : '添加资源'"
          :visible.sync="dialogVisible"
          width="40%">
          <!-- 添加和编辑子组件 -->
          <create-or-edit
          @success="onSuccess"
          @cancel="onCancel"
          :is-edit="isEdit"
          :res-id="resId"
          v-if="dialogVisible"></create-or-edit>
        </el-dialog>
      </el-card>
</template>

<script>
import { getResourcePages, getResourceCategory, getDeleteResource } from '@/services/resource.js'
import CreateOrEdit from './CreateOrEdit'

export default {
  name: 'ResourceList',
  components: {
    CreateOrEdit
  },
  data () {
    return {
      formInline: {},
      resources: [],
      // 分页数据
      form: {
        current: 1,
        size: 10,
        // 资源名称
        name: '',
        // 资源路径
        url: '',
        // 资源分类
        categoryId: ''
      },
      // 数据总条数
      totalCount: 0,
      // 资源分类列表
      resourceCategories: [],
      // 加载状态
      isLoading: true,
      // Dialog对话框
      dialogVisible: false,
      isEdit: false,
      resId: ''
    }
  },
  methods: {
    onSubmit () {
      this.form.current = 1
      this.loadResources()
    },
    // 资源列表数据
    async loadResources () {
      // 开始加载数据
      this.isLoading = true
      const { data } = await getResourcePages(this.form)
      if (data.code === '000000') {
        this.resources = data.data.records
        this.totalCount = data.data.total
      }
      // 请求完毕取消加载状态
      this.isLoading = false
    },
    // 分页功能
    handleSizeChange (val) {
      this.form.size = val
      this.form.current = 1
      this.loadResources()
    },
    handleCurrentChange (val) {
      this.form.current = val
      this.loadResources()
    },
    // 资源分类数据请求
    async loadResourceCategory () {
      const { data } = await getResourceCategory()
      if (data.code === '000000') {
        this.resourceCategories = data.data
      }
    },
    // 重置功能
    onReset () {
      this.$refs.form.resetFields()
    },
    // 添加数据成功(Dialog对话框确定)
    onSuccess () {
      this.dialogVisible = false
      this.loadResources()
    },
    // Dialog对话框取消
    onCancel () {
      this.dialogVisible = false
    },
    // 编辑按钮
    handleEdit (resources) {
      this.dialogVisible = true
      this.isEdit = true
      this.resId = resources.id
    },
    handleAdd () {
      this.dialogVisible = true
      this.isEdit = false
    },
    // 删除按钮
    async handleDelete (id) {
      this.$confirm('确认删除该资源', '删除提示')
        .then(async () => {
          const { data } = await getDeleteResource(id)
          if (data.code === '000000') {
            this.$message.success('删除资源成功')
            this.loadResources()
          }
        }).catch(err => {
          if (err && err.response) {
            this.$message.error('删除失败,请重试')
          } else {
            this.$message.info('取消删除')
          }
        })
    }
  },
  created () {
    // 表单数据
    this.loadResources()
    // 资源分类数据
    this.loadResourceCategory()
  },
  // 过滤器
  filters: {
    dateFormat (date) {
      date = new Date(date)
      return `
        ${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}
        ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}
      `
    }
  }
}
</script>

<style lang="scss" scoped></style>
